package com.mjf.quartz.task;

import cn.hutool.core.util.StrUtil;
import com.mjf.cjt.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/5/22 16:59
 */
@Component("tfTask")
@Slf4j
public class TfTask {

    @Autowired
    private TFSalesOrderService salesOrderService;
    @Autowired
    private TFwarehouseService tFwarehouseService;
    @Autowired
    private TFcontactUnitService tFcontactUnitService;
    @Autowired
    private TFstockService tFstockService;
    @Autowired
    private PurchaseArrivalService purchaseArrivalService;
    @Autowired
    private TFsalerService tFsalerService;
    @Autowired
    private TFReceiptOrderService tfReceiptOrderService;
    @Autowired
    private TfClassService tfClassService;
    @Autowired
    private TmDepartmentDicService tmDepartmentDicService;
    @Autowired
    private TFExpenseInfoService expenseInfoService;
    @Autowired
    private VoucherIncomeService voucherIncomeService;
    @Autowired
    private TOtherInStockOrderService totherInStockOrderService;
    @Autowired
    private TCjtzjbPurchaseArrivalService tCjtzjbPurchaseArrivalService;
    @Autowired
    private SaleDispatchService saleDispatchService;

    @Autowired
    private SaleOrderService saleOrderService;

    @Autowired
    private TCjtExpensevoucherService tCjtExpensevoucherService;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private TCjtzjbSaleDeliveryService tCjtzjbSaleDeliveryService;

    /**
     * 同步销货单
     */
    public void asyncXHD(String start, String end)
    {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("同步销货单开始 {}", LocalDateTime.now());
            salesOrderService.asyncSaleDeliveryOrderFromRemote(start, end);
        }catch (Exception e) {
            log.error("同步销货单异常 ", e);
        }
        log.info("同步销货单结束 {}", LocalDateTime.now());
    }
    /**
     * 仓库档案
     */

    public void asyncCW()
    {
        try {
            log.info("同步仓库档案开始 {}", LocalDateTime.now());
            tFwarehouseService.asyncCW();
        }catch (Exception e) {
            log.error("同步仓库档案异常 ", e);
        }
        log.info("同步仓库档案结束 {}", LocalDateTime.now());
    }
    /**
     * 存货
     */
    public void asyncCH() {
        try {
            log.info("同步存货开始 {}", LocalDateTime.now());
            tFstockService.asyncCH();
        }catch (Exception e) {
            log.error("同步存货异常 ", e);
        }
        log.info("同步存货结束 {}", LocalDateTime.now());
    }

    /**
     * 分类
     */
    public void asyncFL() {
        try {
            log.info("同步分类开始 {}", LocalDateTime.now());
            tfClassService.asyncFL();
        }catch (Exception e) {
            log.error("同步分类异常 ", e);
        }
    }
    /**
     * 往来单位
     */
    public void asyncWLDW() {
        try {
            log.info("同步往来单位开始 {}", LocalDateTime.now());
            tFcontactUnitService.asyncWLDW();
        }catch (Exception e) {
            log.error("同步往来单位异常 ", e);
        }
    }
    /**
     * 员工
     */
    public void asyncYG() {
        try {
            log.info("同步员工开始 {}", LocalDateTime.now());
            tFsalerService.asyncYG();
        }catch (Exception e) {
            log.error("同步员工异常 ", e);
        }
    }
    /**
     * 同步进货单
     */
    public void asyncJHD(String start, String end)
    {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("同步进货单开始 {}", LocalDateTime.now());
            purchaseArrivalService.asyncJHD(start, end);
        }catch (Exception e) {
            log.error("同步进货单异常 ", e);
        }
        log.info("同步进货单结束 {}", LocalDateTime.now());
    }
    /**
     * 收款单
     */
    public void asyncSKD(String start, String end)
    {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("同步收款单开始 {}", LocalDateTime.now());
            tfReceiptOrderService.asyncSKD(start, end);
        }catch (Exception e) {
            log.error("同步收款单异常 ", e);
        }
        log.info("同步收款单结束 {}", LocalDateTime.now());
    }
    /**
     * 费用档案
     */
    public void asyncFYDA() {
        try {
            log.info("同步费用档案开始 {}", LocalDateTime.now());
            expenseInfoService.asyncFYDA();
        }catch (Exception e) {
            log.error("同步费用档案异常 ", e);
        }
    }
    /**
     * 同步部门档案
     */
    public void syncDepart() {
        try {
            log.info("同步部门档案开始 {}", LocalDateTime.now());
            tmDepartmentDicService.syncDepart();
        }catch (Exception e){
            log.error("同步部门档案异常 ", e);
        }
    }
    /**
     * 推送T+费用单
     */
    public void asyncPushExpenseVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("推送T+费用单开始 {}", LocalDateTime.now());
            voucherIncomeService.pushExpenseVoucher(start, end);
        }catch (Exception e){
            log.error("推送T+费用单异常 ", e);
        }
    }
    /**
     * 推送T+其他入库单
     */
    public void asyncTOtherInStockOrderVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("推送T+其他入库单开始 {}", LocalDateTime.now());
            totherInStockOrderService.otherInStockOrder(start, end);
        }catch (Exception e){
            log.error("推送T+其他入库单异常 ", e);
        }
    }

    /**
     * 推送T+进货单
     */
    public void asyncPushProductReceive(String start, String end) {

        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("推送T+进货单开始 {}", LocalDateTime.now());
            tCjtzjbPurchaseArrivalService.asyncPushProductReceive(start, end);
        }catch (Exception e){
            log.error("推送T+进货单异常 ", e);
        }
    }
    /**
     * 销售出库单列表查询
     */
    public void asyncSaleDispatchVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+销售出库单列表查询开始 {}", LocalDateTime.now());
            saleDispatchService.getSaleDispatchOrderFromRemote(start, end);
        }catch (Exception e){
            log.error("T+销售出库单列表查询异常 ", e);
        }
    }

    /**
     * 销售订单列表同步
     */
    public void asyncSaleOrderVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+销售订单列表同步开始 {}", LocalDateTime.now());
            saleOrderService.getSaleOrderFromRemote(start, end);
        }catch (Exception e){
            log.error("T+销售订单列表同步异常 ", e);
        }
    }

    /**
     * 费用单列表同步
     */
    public void asyncExpensevoucherVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+费用单列表同步开始 {}", LocalDateTime.now());
            tCjtExpensevoucherService.getExpensevoucherFromRemote(start, end);
        }catch (Exception e){
            log.error("T+费用单列表同步异常 ", e);
        }
    }
    /**
     * 采购订单列表同步
     */
    public void asyncPurchaseOrderVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("T+采购订单列表同步开始 {}", LocalDateTime.now());
            purchaseOrderService.getPurchaseOrderFromRemote(start, end);
        }catch (Exception e){
            log.error("T+采购订单列表同步异常 ", e);
        }
    }

    /**
     * 推送T+销货单创建
     */
    public void asyncTCjtzjbSaleDeliveryVoucher(String start, String end) {
        try {
            if (StrUtil.isBlank(start)) {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                start = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 00:00:00";
                end = dateTimeFormatter.format(LocalDate.now().minusDays(1)) + " 23:59:59";
            }
            log.info("推送T+销货单创建开始 {}", LocalDateTime.now());
            tCjtzjbSaleDeliveryService.tCjtzjbSaleDelivery(start, end);
        }catch (Exception e){
            log.error("推送T+销货单创建异常 ", e);
        }
    }

}
